<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/css/element.css">
</head>
<script src="/js/Vue.js"></script>
<script src="/js/element.js"></script>
<script src="/js/axios.js"></script>
<script src="/js/Qs.js"></script>
<script src="/js/formDataUtils.js"></script>
<style>
html,body {
    height:100%;
    padding:5px;
}
.el-card {
    height:calc(100% - 2px);
}
#changePassword {
    height:100%;
}
.el-card__body {
    height:calc(100% - 32px);
}
</style>
<body>
<div id="changePassword">
    <el-card>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" style="width:350px;">
            <el-form-item label="原密码" prop="age">
                <el-input v-model.number="ruleForm.age"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</div>
<script>
    var changePassword = new Vue({
        el:"#changePassword",
        data(){
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('原密码不能为空'));
                } else{
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    age: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    age: [
                        { validator: checkAge, trigger: 'blur' }
                    ]
                },

            }
        },
        mounted(){

        },
        methods:{
            submitForm(formName) {
                let that = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        //发送请求
                        that.changePwdRequest();
                    } else {
                        console.log('表单校验失败！');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //修改密码请求
            changePwdRequest(){
                let form = new FormData();
                form.append("newpwd",this.ruleForm.checkPass);
                form.append("oldpwd",this.ruleForm.age);
                axios.post('/user/changepwd',form).then((res)=>{
                    console.log(res);
                    if(res.data.success){
                        this.$message.success("密码修改成功！");
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).catch((err)=>{
                    console.log(err);
                })
            }
        }
    })
</script>
</body>
</html>